package org.example.servicefaculty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.model.manage.bean.po.Counselor;
import org.example.model.manage.bean.po.CounselorMajor;
import org.example.model.manage.bean.po.Faculty;
import org.example.model.manage.bean.po.Major;
import org.example.model.manage.bean.vo.CounselorWithMajorVO;
import org.example.model.manage.bean.vo.MajorVO;
import org.example.servicefaculty.mapper.CounselorMapper;
import org.example.servicefaculty.service.CounselorMajorService;
import org.example.servicefaculty.service.CounselorService;
import org.example.servicefaculty.service.FacultyService;
import org.example.servicefaculty.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CounselorServiceImpl extends ServiceImpl<CounselorMapper, Counselor> implements CounselorService {

    @Autowired
    private CounselorMapper counselorMapper;

    @Autowired
    private CounselorMajorService counselorMajorService;

    @Autowired
    private FacultyService facultyService;

    @Autowired
    private MajorService majorService;

    @Override
    public List<Counselor> getCounselorListByFacultyId(Integer facultyId) {
        // 先根据学院ID获取学院信息
        Faculty faculty = facultyService.getFacultyById(facultyId);
        if (faculty == null) {
            return null;
        }
        // 使用学院的字符串标识查询辅导员
        return counselorMapper.selectList(new QueryWrapper<Counselor>().eq("faculty", faculty.getFaculty()));
    }

    @Override
    public List<CounselorWithMajorVO> getCounselorWithMajorByFacultyId(Integer facultyId) {
        // 获取学院信息
        Faculty faculty = facultyService.getFacultyById(facultyId);
        if (faculty == null) {
            return null;
        }

        // 获取该学院的所有辅导员
        List<Counselor> counselors = counselorMapper.selectList(new QueryWrapper<Counselor>().eq("faculty", faculty.getFaculty()));

        // 转换为包含专业信息的VO列表
        return counselors.stream().map(counselor -> {
            CounselorWithMajorVO vo = new CounselorWithMajorVO();
            vo.setId(counselor.getId());
            vo.setName(counselor.getName());
            vo.setFaculty(counselor.getFaculty());

            // 获取专业详细信息
            List<Major> majors = counselorMajorService.getMajorByCounselorId(counselor.getId());

            // 转换为MajorVO列表
            List<MajorVO> majorVOs = majors.stream().map(major -> {
                MajorVO majorVO = new MajorVO();
                majorVO.setId(major.getId());
                majorVO.setMajor(major.getMajor());
                majorVO.setFacultyId(major.getFacultyId());
                return majorVO;
            }).collect(Collectors.toList());

            vo.setMajorList(majorVOs);
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public Counselor getCounselorById(Integer counselorId) {
        return getById(counselorId);
    }

    @Override
    public Boolean createCounselor(Counselor counselor) {
        return save(counselor);
    }

    @Override
    public Boolean updateCounselor(Counselor counselor) {
        return updateById(counselor);
    }

    @Override
    public List<Counselor> getByFacultyId(Integer facultyId) {
        // 复用已有的方法实现
        return getCounselorListByFacultyId(facultyId);
    }

    @Override
    public Boolean updateCounselorMajor(Integer majorId, List<Integer> counselorIds) {
        // 先解绑该majorId与所有辅导员的关系
        QueryWrapper<CounselorMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("major_id", majorId);
        counselorMajorService.remove(queryWrapper);

        // 再将该majorId与队列中的辅导员进行绑定
        List<CounselorMajor> counselorMajors = counselorIds.stream()
                .map(counselorId -> new CounselorMajor()
                        .setCounselorId(counselorId)
                        .setMajorId(majorId))
                .collect(Collectors.toList());

        return counselorMajorService.saveBatch(counselorMajors);
    }

    @Override
    public Boolean updateCounselorMajors(Integer counselorId, List<Integer> majorIds) {
        // 先解绑该辅导员与所有专业的关系
        QueryWrapper<CounselorMajor> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("counselor_id", counselorId);
        counselorMajorService.remove(queryWrapper);

        // 再将该辅导员与队列中的专业进行绑定
        if (majorIds != null && !majorIds.isEmpty()) {
            List<CounselorMajor> counselorMajors = majorIds.stream()
                    .map(majorId -> new CounselorMajor()
                            .setCounselorId(counselorId)
                            .setMajorId(majorId))
                    .collect(Collectors.toList());
            return counselorMajorService.saveBatch(counselorMajors);
        }
        
        return true; // 如果专业列表为空，则只解绑，返回成功
    }
}